﻿@page "/StepLines/Properties"
@using LiveChartsCore.SkiaSharpView.Blazor
@using LiveChartsCore.SkiaSharpView
@using LiveChartsCore
@using LiveChartsCore.SkiaSharpView.Painting
@using SkiaSharp

<div>
	<button type="button" class="btn btn-primary" @onclick="ChangeValuesInstance">Values</button>
	<button type="button" class="btn btn-primary" @onclick="ChangeSeriesInstance">Series</button>
	<button type="button" class="btn btn-primary" @onclick="NewFill">Fill</button>
	<button type="button" class="btn btn-primary" @onclick="NewStroke">Stroke</button>
	<button type="button" class="btn btn-primary" @onclick="NewGeometryFill">Geometry fill</button>
	<button type="button" class="btn btn-primary" @onclick="NewGeometryStroke">Geometry stroke</button>
	<button type="button" class="btn btn-primary" @onclick="IncreaseGeometrySize">+ geo size</button>
	<button type="button" class="btn btn-primary" @onclick="DecreaseGeometrySize">- geo size</button>
</div>

<CartesianChart
	Series="Series">
</CartesianChart>

@code {
    private StepLineSeries<double> _stepLineSeries;
    private ISeries[] Series { get; set; }
    private Random _random = new();

    protected override void OnInitialized()
    {
        _stepLineSeries = new StepLineSeries<double>
        {
            Values = new double[] { -2, -1, 3, 5, 3, 4, 6 },
            Stroke = new SolidColorPaint(SKColors.Black),
            Fill = new SolidColorPaint(SKColor.Parse("#30000000")),
            GeometryStroke = new SolidColorPaint(SKColors.Black),
            GeometryFill = new SolidColorPaint(SKColor.Parse("#30000000")),
            GeometrySize = 20
        };

        Series = new ISeries[] { _stepLineSeries };
    }

    private void ChangeValuesInstance()
    {
        var t = 0;
        var values = new double[10];
        for (var i = 0; i < 10; i++)
        {
            t += _random.Next(-5, 10);
            values[i] = t;
        }
        _stepLineSeries.Values = values;
        StateHasChanged();
    }

    private void NewStroke()
    {
        _stepLineSeries.Stroke = new SolidColorPaint(GetRandomColor());
        StateHasChanged();
    }

    private void NewFill()
    {
        _stepLineSeries.Fill = new SolidColorPaint(GetRandomColor());
        StateHasChanged();
    }

    private void NewGeometryFill()
    {
        _stepLineSeries.GeometryFill = new SolidColorPaint(GetRandomColor());
        StateHasChanged();
    }

    private void NewGeometryStroke()
    {
        _stepLineSeries.GeometryStroke = new SolidColorPaint(GetRandomColor());
        StateHasChanged();
    }

    private void IncreaseGeometrySize()
    {
        if (_stepLineSeries.GeometrySize >= 60) return;
        _stepLineSeries.GeometrySize += 10;
        StateHasChanged();
    }

    private void DecreaseGeometrySize()
    {
        if (_stepLineSeries.GeometrySize <= 0) return;
        _stepLineSeries.GeometrySize -= 10;
        StateHasChanged();
    }

    private void ChangeSeriesInstance()
    {
        _stepLineSeries = new StepLineSeries<double>
        {
            Values = new double[] { -2, -1, 3, 5, 3, 4, 6 },
            Stroke = new SolidColorPaint(SKColors.Black),
            Fill = new SolidColorPaint(SKColor.Parse("#30000000")),
            GeometryStroke = new SolidColorPaint(SKColors.Black),
            GeometryFill = new SolidColorPaint(SKColor.Parse("#30000000")),
            GeometrySize = 20
        };
        Series = new ISeries[] { _stepLineSeries };
        StateHasChanged();
    }

    private SKColor GetRandomColor()
    {
        var r = _random.Next(0, 255);
        var g = _random.Next(0, 255);
        var b = _random.Next(0, 255);
        return new SKColor((byte)r, (byte)g, (byte)b);
    }
}
